package com.bwie.shop.controller;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bwie.shop.dao.ShopDao;
import com.bwie.shop.entity.ShopEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bwie.shop.entity.ActiveEntity;
import com.bwie.shop.service.ActiveService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 *
 * @author shop
 * @email sunlightcs@gmail.com
 * @date 2022-06-19 15:32:35
 */
@RestController
@RequestMapping("shop/active")

public class ActiveController {
    @Autowired
    private ActiveService activeService;

    @Autowired
    private ShopDao shopDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = activeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{activeId}")
    public R info(@PathVariable("activeId") Integer activeId){
		ActiveEntity active = activeService.getById(activeId);

        return R.ok().put("active", active);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ActiveEntity active){
        ShopEntity shopEntity = shopDao.selectById(active.getSid());
        active.setPrc(shopEntity.getPrc());
		activeService.save(active);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ActiveEntity active){
		activeService.updateById(active);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] activeIds){
		activeService.removeByIds(Arrays.asList(activeIds));

        return R.ok();
    }

    @RequestMapping("/excelOut")
    public void getExcel(HttpServletRequest request, HttpServletResponse response) {
        //获取数据
        List<ActiveEntity> list = activeService.list();
        ArrayList<ActiveEntity> arr = new ArrayList<>();
        for (ActiveEntity physicianEntity : list) {
            arr.add(JSONObject.parseObject(JSON.toJSONString(physicianEntity),new TypeReference<ActiveEntity>(){}));
        }
        System.out.println(arr);
        try {
            long l = System.currentTimeMillis();
            String fileName = String.valueOf(l);
            //EasyExcel.write(out, ExcelPhysician.class).autoCloseStream(true).sheet("医师信息").doWrite(arr);
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "utf-8"));
            //
            EasyExcel.write(response.getOutputStream(), ActiveEntity.class).autoCloseStream(Boolean.FALSE).sheet("秒杀活动信息")
                    .doWrite(arr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
